<template>
  <div>
    <my-breadcrumb :items="['用户管理', '系统运营人员管理']"/>
    <search :formItems="searchItems" ref="search" @search="getList"/>
    <div class="add">
      <el-button type="primary" @click="toAdd">新增管理</el-button>
    </div>
    <el-table :data="systems">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        label="头像"
        width="70px"
        prop="name">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" alt="" height="40">
        </template>
      </el-table-column>
      <el-table-column
        label="名称"
        prop="name">
      </el-table-column>
      <el-table-column
        label="角色组"
        prop="rolename">
      </el-table-column>
      <el-table-column
        label="账号"
        prop="phone_num">
      </el-table-column>
      <el-table-column
        label="状态"
        prop="status">
        <template slot-scope="scope">
          {{ scope.row.status === '1' ? '启用' : '不启用' }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        prop="id">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="toDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog :title="isAdd ? '添加人员' : '信息修改'" :visible.sync="showAdd" width="500px">
      <el-form :model="user" :rules="rules" label-width="90px" ref="addForm">
        <el-form-item label="头像" prop="avatar">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :before-upload="beforeUpload">
            <img v-if="user.avatar" :src="user.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="user.name"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="user.password" type="password"/>
        </el-form-item>
        <el-form-item label="账号" prop="phone_num">
          <el-input v-model="user.phone_num"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="user.status">
            <el-option value="0" label="禁用"/>
            <el-option value="1" label="启用"/>
          </el-select>
        </el-form-item>
        <el-form-item label="角色组" prop="roleid">
          <el-select v-model="user.roleid">
            <el-option v-for="(item, index) in roles" :key="index" :value="item.id" :label="item.name"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button type="primary" @click="toSubmit">提交</el-button>
        <el-button type="danger" @click="showAdd = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getRole} from '../../utils/getList'
export default {
  name: 'system',
  data () {
    return {
      searchItems: [
        {
          label: '名称',
          type: 'input',
          key: 'name'
        }
      ], // 检索结构
      systems: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      isAdd: true,
      showAdd: false,
      user: {
        name: '',
        password: '',
        phone_num: '',
        status: '',
        avatar: '',
        roleid: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        // password: [
        //   { required: true, message: '请输入密码', trigger: 'blur' }
        // ],
        phone_num: [
          { required: true, message: '请输入电话号码', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ],
        avatar: [
          { required: true, message: '请输入头像', trigger: 'blur' }
        ],
        roleid: [
          { required: true, message: '请选择角色分组', trigger: 'change' }
        ]
      },
      roles: [],
      updateRow: null
    }
  },
  methods: {
    getList () {
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      let params = {...searchData, page: this.page.pageNum, size: this.page.pageSize, orderby: this.lifting}
      this.axios.get('/Rbac/userindex', {params})
        .then(res => {
          if (res.data.status) {
            this.page.total = Number(res.data.data.count)
            this.systems = res.data.data.list
          }
        })
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList()
    },
    handleCurrentChange (e) {
      this.page.pageNum = e
      this.getList()
    },

    beforeUpload (file) {
      let reader = new FileReader()
      let _self = this
      reader.onload = function (ev) {
        _self.user.avatar = ev.currentTarget.result
      }
      reader.readAsDataURL(file)
      return false
    },

    toAdd () {
      this.isAdd = true
      this.showAdd = true
      this.$nextTick(() => {
        this.$refs['addForm'].resetFields()
        this.$refs['addForm'].clearValidate()
      })
    },
    toUpdate (row) {
      this.isAdd = false
      this.updateRow = row
      this.showAdd = true
      this.$nextTick(() => {
        Object.keys(row).forEach(key => {
          if (this.user[key] !== undefined) {
            this.user[key] = row[key]
          }
          if (key === 'password') {
            this.user.password = ''
          }
        })
        this.$refs['addForm'].clearValidate()
      })
    },
    toSubmit () {
      let params = {}
      let url = '/Rbac/useradd'
      Object.keys(this.user).forEach(key => {
        params[key] = this.user[key]
      })
      if (!this.isAdd) {
        params.id = this.updateRow.id
        url = '/Rbac/useredit'
      }
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          this.axios.post(url, this.qs.stringify(params))
            .then(res => {
              if (res.data.status === 1) {
                this.$message.success(res.data.msg)
                this.showAdd = false
                this.getList()
              }
            })
        } else {
          this.$message.error('请检查表单是否填写正确')
          return false
        }
      })
    },

    toDelete (id) {
      this.$confirm('此操作将删除该管理人员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.get('/Rbac/deluser', {params: {id}})
          .then(res => {
            if (res.data.status === 1) {
              this.getList()
            }
          })
      })
    }
  },
  created () {
    this.getList()
    getRole(data => {
      this.roles = data
    })
  }
}
</script>

<style scoped>
  .avatar-uploader-icon:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
